#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

export FERRIUM_URL=https://atomgit.com/m-robots/robot_tools_ferrium
export FERRIUM_PKGS_URL=https://atomgit.com/m-robots/robot_tools_ferrium_pkgs
export FERRIUM_DIR=$HOME/.cache/ferrium-repo

# clone ferrium
if [ ! -d ${FERRIUM_DIR} ]; then
    git clone ${FERRIUM_URL} ${FERRIUM_DIR}
else
    echo "updating ferrium"
    cd ${FERRIUM_DIR}
    git pull origin ohos
    cd -
fi

# build ferrium
echo "building ferrium"
cd ${FERRIUM_DIR}
cargo build --release || exit 1
export PATH=$PATH:${FERRIUM_DIR}/target/release

# init ferrium config
echo "initializing ferrium config"
export CONFIG_ROOT=$HOME/.cache/ferrium-config
ferrium init \
    --config-root ${CONFIG_ROOT} \
    --pkgs-root /pkg \
    --shell-configs \
    || exit 1
